package com.example.farmeri.presenter.user;

import com.example.farmeri.http.API;
import com.example.farmeri.pojo.Role;
import com.example.farmeri.presenter.base.BasePresenter;
import com.example.farmeri.tools.ResponseUtils;

import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
import io.reactivex.rxjava3.annotations.NonNull;
import io.reactivex.rxjava3.core.Observer;
import io.reactivex.rxjava3.disposables.Disposable;

public class UsersInfoPresenter extends BasePresenter<IUsersInfoContract> {
    protected final String TAG = this.getClass().getSimpleName();

    public UsersInfoPresenter(IUsersInfoContract view) {
        super(view);
    }

    public void lockUser(String id){
        getRetrofit()
                .create(API.class)
                .lockUser(id)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseUtils>() {
                    @Override
                    public void onSubscribe(@NonNull Disposable d) {

                    }

                    @Override
                    public void onNext(@NonNull ResponseUtils responseUtils) {
                        if (responseUtils.getCode() == ResponseUtils.CODE_SUCCESS){
                            getView().onLockUserSuccess();
                        }else {
                            getView().onError(responseUtils.getMessage());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        getView().onError("网络连接失败");
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public void deleteUser(String id){
        getAPI().deleteUser(id).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<ResponseUtils<Role>>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {

            }

            @Override
            public void onNext(@NonNull ResponseUtils<Role> roleResponseUtils) {
                if (roleResponseUtils.getCode() == ResponseUtils.CODE_SUCCESS){
                    getView().onDeleteUserSuccess();
                }else {
                    getView().onError(roleResponseUtils.getMessage());
                }

            }

            @Override
            public void onError(@NonNull Throwable e) {
                showErrorMsg();
            }

            @Override
            public void onComplete() {

            }
        });
    }
}
